

console.log("ozon comment load");

chrome.runtime.onMessage.addListener(async (msg, sender) => {

   //测试下线商品
  //拿到 asyncData https://www.ozon.ru/api/entrypoint-api.bx/page/json/v2?url=%2Fproduct%2Fpatchi-dlya-glaz-iz-morskih-vodorosley-gidrogelevaya-maska-ot-meshkov-sinyakov-i-morshchin-pod-1056706679%2F%3Flayout_container%3DpdpReviews%26layout_page_index%3D2%26sh%3DKh_TxUq28A%26
  // post 请求 https://www.ozon.ru/api/composer-api.bx/widget/json/v2?widgetStateId=webReviewProductScore-3131802-pdpReviews-2

    console.log(msg);
    getComment(msg);

})




function getComment(msg){
    var sku_id = msg.data.sku_id;
    if(!sku_id){
        return;
    }
    /*sku_id = 1312246414;
    msg.data.sku_id =1312246414*/
    var urlGetParams = "/product/"+msg.data.sku_id+"?layout_container=pdpReviews&layout_page_index=2";
    var urlParam= encodeURIComponent(urlGetParams);
    var url ="https://www.ozon.ru/api/entrypoint-api.bx/page/json/v2?url="+urlParam;
    console.log(url);
    var asyncData = null;
    $.ajax({
      url: url,
      method: 'GET',
      success: function(data) {
         console.log(data);
         if(data['layout']){
                data['layout'].forEach((item1) => {
                if(item1.component='webReviewTabs'){
                    if(!item1.placeholders){return;}
                    item1.placeholders.forEach((item2) => {
                         if(item2.name=='tabs'){
                             if(!item2.widgets){return;}
                             item2.widgets.forEach((item3) => {
                                    if(!item3.placeholders){return;}
                                    item3.placeholders.forEach((item4) => {
                                        if(!item4.widgets){return;}
                                        item4.widgets.forEach((item5) => {
                                            if(!item5.placeholders){return;}
                                            item5.placeholders.forEach((item6) => {
                                                    if(!item6.widgets){return;}
                                                    item6.widgets.forEach((item7) => {
                                                        if(item7.component=='webReviewProductScore'){
                                                                console.log(item7)
                                                                asyncData = item7.asyncData;
                                                        }
                                                    });
                                            });

                                        });
                                    });
                             });
                         }
                    });
                }
            });
         }

       console.log(asyncData);


       if(!asyncData){
            console.log("asyncData null send message");
            var listComment = [];
            var body = listComment;
            var type = "updateOzonComment";
            chrome.runtime.sendMessage({ data: {type , body ,sku_id}, close: false });
            return;
       }
        var asydata = {};
        asydata.asyncData = asyncData;

        $.ajax({
          type: "POST",
          url: "https://www.ozon.ru/api/composer-api.bx/widget/json/v2?widgetStateId=webReviewProductScore-3131802-pdpReviews-2",
          data: JSON.stringify(asydata),
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function(response) {
            console.log(response);

            var listComment = [];

               response.state.score.forEach((item) => {
                    listComment.push(item.value);
                });
            console.log(listComment);
            var body = listComment;
            var type = "updateOzonComment";
            chrome.runtime.sendMessage({ data: {type , body ,sku_id}, close: false });

          }
        });
      },
      error: function(xhr, status, error) {
        console.log('Ajax error: ', error);
        if (xhr.status === 403 && status === "error") {
            var type="ozonapi403";
            chrome.runtime.sendMessage({ data: {type}, close: false });
            console.log("遇到了403错误");
        }else{
            getComment(msg)
        }
      }
    });

}

function getAsyncData(){



}



/*


console.log("get comment load!")

window.addEventListener('load', function() {
    const timerId = setInterval(() => {
        var webReviewTabs = $('div[data-widget="webReviewTabs"]').find(".yr").html();
        if(webReviewTabs ){
            clearInterval(timerId);
            getDataComment();
        }else{
            console.log("wait !");
        }
    },1000);
});


function getDataComment(){
   //能有这个元素表示评论数据已经加载
   var yr = $('div[data-widget="webReviewTabs"]').find(".yr");
   console.log(yr.html())
   var listComment = [];
   console.log(yr.children().next().html())
   yr.children().eq(1).children().each(function(e){
        listComment.push($(this).children().last().html());
    })

    console.log(listComment);
    var body = listComment;
    var type = "updateOzonComment";
    var url = location.href;
    chrome.runtime.sendMessage({ data: {type , body ,url}, close: false });
}*/
